package com.nondo.dean.platform.controller.api.inner;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.web.View;
import com.nondo.dean.platform.domain.ProductDiscount;
import com.nondo.dean.platform.model.param.ProductPriceParam;
import com.nondo.dean.platform.model.result.PriceResult;
import com.nondo.dean.platform.model.result.ProductDiscountResult;
import com.nondo.dean.platform.service.ProductDiscountService;

/**
 * 产品价格管理内部接口
 * @author zlj
 * @date 2018年4月4日
 */
@RestController("apiInnerproductDiscount")
@RequestMapping("/{v}/inner/price")
public class ProductDiscountController {
    
    @Autowired
    ProductDiscountService productDiscountService;
    
    /**
     * 查看特殊产品价格详情
     * @param request
     * @param response
     * @param id 主键ID
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<ProductDiscountResult> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<ProductDiscountResult> view = new View<ProductDiscountResult>(20000, "获取数据成功");
        
        ProductDiscountResult productDiscountResult = null;
        if (id != null) {
            ProductDiscount productDiscountDb = productDiscountService.selectProductDiscount(id);
            productDiscountResult = productDiscountService.getProductDiscountResult(productDiscountDb);
        } else {
            productDiscountResult = new ProductDiscountResult();
        }

        view.setData(productDiscountResult);
        return view;
    }
    
    /**
     * 根据产品和经销商ID查询产品折扣率
     * @param request
     * @param response
     * @param userId 经销商ID
     * @param productId 产品ID
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{userId}/{productId}")
    public View<ProductDiscount> getproductDiscountByUser(HttpServletRequest request, HttpServletResponse response, @PathVariable("userId") Integer userId, @PathVariable("productId") Integer productId) {
        View<ProductDiscount> view = new View<ProductDiscount>(20000, "获取数据成功");
        
        ProductDiscount productDiscount = null;
        productDiscount = productDiscountService.getProductDiscountByUserAndProductId(userId, productId);

        view.setData(productDiscount);
        return view;
    }
    
    /**
     * 根据区域和经销商等级计算产品价格
     * @param request
     * @param response
     * @param param 封装了区域和经销商等级的VO对象
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/compute")
    public View<PriceResult> getproductDiscountByZone(HttpServletRequest request, HttpServletResponse response, @RequestBody ProductPriceParam param) {
        View<PriceResult> view = new View<PriceResult>(20000, "获取数据成功");
        
        PriceResult priceResult = null;
        priceResult = productDiscountService.getProductDiscount(param);

        view.setData(priceResult);
        return view;
    }
    
}